package com.zjzywl.smoke.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
/*
 * 响应速度
 */
import java.util.List;
import java.util.Map;

import javax.websocket.server.PathParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zjzywl.smoke.authenticate.AuthUser;
import com.zjzywl.smoke.repository.AlarmRepository;
import com.zjzywl.smoke.util.WebResult;

@RestController
@RequestMapping(value = "/response")
public class ResponseSpeedController extends BaseController {
	@Autowired
	private AlarmRepository repository;

	// 获取响应速度
	@RequestMapping(value = "/Speed")
	public WebResult getAlarmTypes(@PathParam("beginTime") String beginTime, @PathParam("endTime") String endTime,
			@PathParam("alarmCode") String alarmCode) throws ParseException {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date bTime = format.parse(beginTime);
		Date eTime = format.parse(endTime);
		AuthUser userId = this.getLoginUser();
		String disposePeople = userId.getPeople().getName();
		System.out.println(disposePeople + "disposePeople");
		List alarmTypes = repository.findByTimeDifference(bTime, eTime, alarmCode, disposePeople);
		if (alarmTypes != null) {
			Map map = getMap(alarmTypes);
			return WebResult.OK(map);
		} else {
			return WebResult.FAIL("同比参数出错");
		}
	}
	
	//整理返回值的格式
	private Map getMap(List list) {
		int a = 0;
		int b = 0;
		int c = 0;
		int e = 0;
		int f = 0;
		int g = 0;
		for (Object object : list) {
			String value = object + "";
			double d = Double.parseDouble(value);
			if (d > 0 && d <= 0.5) {
				a++;
			} else if (d > 0.5 && d <= 1) {
				b++;
			} else if (d > 1 && d <= 1.5) {
				c++;
			} else if (d > 1.5 && d <= 2) {
				e++;
			} else if (d > 2 && d <= 2.5) {
				f++;
			} else if (d > 2.5 && d <= 3) {
				g++;
			}
		}
		Map<String, Integer> map = new HashMap<>();
		map.put("0.5", a);
		map.put("1.0", b);
		map.put("1.5", c);
		map.put("2.0", e);
		map.put("2.5", f);
		map.put("3.0", g);

		return map;

	}
}
